package com.example.chenzhiran.myapplication7.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.Statement;

public class purchasePage {
    private Socket socket;
    private clientData indata;
    private serverData outdata = new serverData();
    ObjectOutputStream out;
    Statement statement;

    public purchasePage(Socket mysocket, clientData mydata, Statement stmt)
    {
        this.socket = mysocket;
        this.indata = mydata;
        this.statement = stmt;

        switch(indata.dataId)
        {
            case 0:
                addPurchase();
                break;
            case 1:
                getHistory();
                break;
            case 2:
                cancelPurchase();
                break;
            case 3:
                getStatus();
                break;
        }

        try
        {
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(outdata);
            out.close();
            System.out.printf("购买页数据已发送\n");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    /*描述：添加购买记录
     *参数：空
     *返回：空
     */
    public void addPurchase()
    {
        boolean finduser = false;
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                String name = rs.getString("username");
                if(name.equals(indata.username))
                {
                    finduser = true;
                    break;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        if(finduser == false)
        {
            outdata.datastatus = 1;
            return;
        }

        try
        {
            String[] strs = indata.inputmessage.split(" ");
            String sql = "insert into purchasehistory (username, product, status, address) value (" + "\"" + indata.username + "\"" + "," + "\"" + strs[1] + "\"" + "," + "\"" + "proceeding" + "\"" + "," + "\"" + strs[0] + "\"" +")";
            int rs = statement.executeUpdate(sql);
            outdata.datastatus = 0;
        }catch(Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }

    }

    /*描述：获取购买历史
     *参数：空
     *返回：空
     */
    public void getHistory()
    {
        boolean finduser = false;
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                String name = rs.getString("username");
                if(name.equals(indata.username))
                {
                    finduser = true;
                    break;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        if(finduser == false)
        {
            outdata.datastatus = 1;
            return;
        }

        String sql = "select * from purchasehistory where username = \"" + indata.username + "\"";
        String[] paths = new String[10];
        int pos = 0;
        try {
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String path = rs.getString("product");
                paths[pos++] = path;
                FileInputStream fin = new FileInputStream(new File(path));
                byte[] pic = new byte[fin.available()];
                fin.read(pic);
                outdata.imgs.put(path, pic);
                fin.close();
                outdata.datastatus = 0;
            }
            rs.close();

            getInfo(paths, pos);
        }catch (Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }
    }

    /*描述：取消购买
     *参数：空
     *返回：空
     */
    public void cancelPurchase()
    {
        if(indata.inputmessage == null  || indata.inputmessage.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }
        else if(indata.username == null || indata.username.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }

        boolean finduser = false;
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                String name = rs.getString("username");
                if(name.equals(indata.username))
                {
                    finduser = true;
                    break;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        if(finduser == false)
        {
            outdata.datastatus = 1;
            return;
        }

        try
        {
            String sql = "delete from likelist where product = " + "\"" +  indata.inputmessage  + "\"" +  " and username = " +  "\"" +  indata.username  + "\"";
            int rs = statement.executeUpdate(sql);
            outdata.datastatus = 0;
        }catch(Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }
    }

    /*描述：获取商品购买状态
     *参数：空
     *返回：空
     */
    public void getStatus()
    {

    }

    /*描述：获取商品描述信息
     *参数：空
     *返回：空
     */
    public void getInfo(String[] paths, int pos)
    {
        for(int i=0; i<pos; i++) {
            String path = paths[i];
            char[] picpath = path.toCharArray();
            char gender = picpath[10];
            String table;
            if (gender == 'm') {
                table = "malepic";
            } else table = "femalepic";

            String mysql = "select * from " + table + " where loc = \"" + path + "\"";
            try {
                ResultSet result = statement.executeQuery(mysql);
                while (result.next()) {
                    String describe = result.getString("des");
                    String sprice = result.getString("pri");
                    outdata.productdescribes.put(path, describe);
                    outdata.productprices.put(path, Integer.parseInt(sprice));
                }
                result.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
